package com.rrd.hive.udf;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.hadoop.hive.ql.exec.UDF;

/**
*人行上报V2版，计算上报利息服务费
*@author liubaoxin
*@date 2020年07月02日
*/
public class RHZXInterestMgmtUDF  extends UDF{
	Pattern pattern = Pattern.compile("#"); 
  
	public Map<String,Double> evaluate(Map<String,String> map)
            throws UnsupportedEncodingException {
		if(map==null || map.size()<=0){
			return null;
		}
		String arr[]; 
		Map<String,Double> m = new HashMap<String,Double>();
		double apr36_fee=0;
		double due_fee=0;
		double addup_fee=0;
		for(int i=1;i<=map.size();i++){
			arr = pattern.split(map.get(String.valueOf(i)));
			apr36_fee=Double.parseDouble(arr[0]);
			due_fee=Double.parseDouble(arr[1]);
			if(apr36_fee-addup_fee>=due_fee){
				m.put(String.valueOf(i), due_fee);
			}else{
				m.put(String.valueOf(i), apr36_fee-addup_fee);
			}
			addup_fee+=m.get(String.valueOf(i));
		}
        return m;
    }
	public static void main(String[] args) throws UnsupportedEncodingException {
		 Map<String,String> map = new HashMap<String,String>();
		 map.put("1", "599.0164#423.75");
		 map.put("2", "1041.1475#348.97");
		 map.put("3", "1469.0164#348.97");
		 map.put("4", "1911.1475#58.97");
		 map.put("5", "2339.0164#58.97");
		 map.put("6", "2781.1475#53.17");
		 RHZXInterestMgmtUDF udf = new RHZXInterestMgmtUDF();
		 System.out.println(udf.evaluate(map));;
	}
}
